import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/entities/summary_child_entity.dart';
import 'package:library_base/entities/summary_entity.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/image/local_image.dart';

Widget buildSummaryItem(
  SummaryEntity model, {
  Callback<SummaryEntity>? onTap,
  Callback<SummaryChildEntity>? onTapChild,
  Callback<SummaryEntity>? onExpand,
}) {
  List<Widget> widgets = [];

  double totalWeight = NumUtil.multiply(model.totalCount,model.standardWeight);

  widgets.add(GestureDetector(
    onTap: () {
      onTap?.call(model);
    },
    child: Container(
      color: Colors.transparent,
      padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 24.w),
      child: Row(
        children: [
          Padding(
              padding: EdgeInsets.only(right: 16.w),
              child: LocalImage(
                  model.isSelect ? 'admin_checked' : 'admin_no_checked',
                  package: Constant.packAdmin(),
                  width: 34.r,
                  height: 34.r)),
          Expanded(
              child: SizedBox(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                //商品标题和货主
                Row(
                  children: [
                    AutoSizeText(
                      model.goodsName,
                      minFontSize: 1,
                      maxFontSize: 100,
                      style: TextStyle(
                          fontSize: 30.sp, fontWeight: FontWeight.bold),
                    ),
                    gapW16,
                    Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 10.w, vertical: 4.w),
                      decoration: BoxDecoration(
                          color: ThemeUtil.getTheme().secondaryBgColor(),
                          borderRadius: BorderRadius.circular(3)),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          AutoSizeText(
                            MultiLanguage.consignor.tr + '：',
                            overflow: TextOverflow.ellipsis,
                            minFontSize: 1,
                            maxFontSize: 100,
                            style: TextStyle(
                                fontSize: 24.sp,
                                color: ThemeUtil.getTheme().sixthTextColor()),
                          ),
                          AutoSizeText(
                            model.shipperName,
                            overflow: TextOverflow.ellipsis,
                            minFontSize: 1,
                            maxFontSize: 100,
                            style: TextStyle(
                                fontSize: 24.sp,
                                color: ThemeUtil.getTheme().sixthTextColor()),
                          )
                        ],
                      ),
                    )
                  ],
                ),
                //目前业务未完善库存数暂时隐藏
                Visibility(
                  visible: false,
                  child: Row(
                    children: [
                      Expanded(
                        child: Row(children: [
                          AutoSizeText(
                            '${MultiLanguage.currentInventory.tr}：',
                            minFontSize: 1,
                            maxFontSize: 100,
                            style: sixthTextColor28,
                          ),
                          Expanded(
                            child: Text(
                              '${model.orderDetails.length}',
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: normalTextColor28,
                            ),
                          ),
                        ]),
                      ),
                    ],
                  ),
                ),
                gapH18,
                SizedBox(
// height: 30.w,
                  child: Row(
                    children: [
                      Expanded(child: Row(children: [
                        AutoSizeText(
                          '${MultiLanguage.requiredQuantity.tr} : ',
                          minFontSize: 1,
                          maxFontSize: 100,
                          style: sixthTextColor28,
                        ),
                        Expanded(
                          child: AutoSizeText(
                            '${model.totalCount}',
                            minFontSize: 1,
                            maxFontSize: 100,
                            style: normalTextColor28,
                          ),
                        ),
                      ])),
                      Expanded(child: Row(children: [
                        AutoSizeText(
                          '${MultiLanguage.weightKg.tr}${SPUtil.isJin.unitStrZhWithBracket()} : ',
                          minFontSize: 1,
                          maxFontSize: 100,
                          style: sixthTextColor28,
                        ),
                        Expanded(
                          child: AutoSizeText(
                            totalWeight.remoteUnitTransitionLocalUnitStr(model.goodsUnit),
                            minFontSize: 1,
                            maxFontSize: 100,
                            style: normalTextColor28,
                          ),
                        ),
                      ]))
                    ],
                  ),
                ),
              ],
            ),
          )),
          GestureDetector(
            onTap: () {
              onExpand?.call(model);
            },
            child: Container(
              color: Colors.transparent,
              padding: EdgeInsets.symmetric(vertical: 16.w),
              child: LocalImage(
                model.isExpand ? 'admin_expand_close' : 'admin_expand_open',
                width: 35.w,
                height: 35.w,
                package: Constant.packAdmin(),
              ),
            ),
          )
          // LayoutBuilder(
          //     builder:(context,constraints){
          //       logger.i('组件高度 = ${constraints.maxHeight}');
          //
          //       //详情的高度
          //       var widgetsBinding = WidgetsBinding.instance;
          //       widgetsBinding?.addPostFrameCallback((callback) {
          //         gdHeight =context.size.height;
          //         print("详情的高度：${context.size.height}");
          //       }
          //
          //
          //       return ;
          //     }
          // )
        ],
      ),
    ),
  ));

  widgets.add(Visibility(
    visible: model.isExpand,
    child: line1,
  ));

  var childWidgets = model.orderDetails.buildItem((index, orderDetail) {
    return GestureDetector(
        onTap: () {},
        child: Container(
            padding: EdgeInsets.symmetric(vertical: 19.w, horizontal: 24.w),
            child: buildSummaryChildItem(orderDetail, onTap: onTapChild)));
  }, dividingLine: () {
    return line1;
  }, divideType: DivideType.MIDDLE);

  widgets.add(Visibility(
    visible: model.isExpand,
    child: Column(
      children: childWidgets,
    ),
  ));

  childWidgets.add(Visibility(
    visible: model.isExpand,
    child: Container(
      color: ThemeUtil.getTheme().dividingLineColor(),
      width: double.infinity,
      height: 0.7,
    ),
  ));
  Widget widget = roundWidget(
    margin: edgeLR24,
    decoration: DecorationRes.rs8,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: widgets,
    ),
  );
  return widget;
}

Widget buildSummaryChildItem(SummaryChildEntity model,
    {Callback<SummaryChildEntity>? onTap}) {
  return GestureDetector(
      onTap: () {
        onTap?.call(model);
      },
      child: Container(
        color: Colors.transparent,
        child: Row(children: [
          Padding(
              padding: EdgeInsets.only(right: 16.w),
              child: LocalImage(
                  model.isSelect ? 'admin_checked' : 'admin_no_checked',
                  package: Constant.packAdmin(),
                  width: 35.w,
                  height: 35.w)),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(children: [
                  Expanded(
                      child: Row(
                    children: [
                      Text(MultiLanguage.consignee.tr, style: sixthTextColor28_1),
                      Text(' : ', style: sixthTextColor28),
                      Expanded(
                          child:
                              Text(model.consignee, style: normalTextColor28)),
                    ],
                  )),
                  Expanded(
                      child: Row(
                    children: [
                      Text("${MultiLanguage.quantity.tr} : ",
                          style: sixthTextColor28),
                      Expanded(
                          child: Text(model.goodsCount.toString(),
                              style: normalTextColor28)),
                    ],
                  )),
                ]),
                gapH8,
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                  Text("${MultiLanguage.shortName.tr} : ", style: sixthTextColor28_3),
                  Expanded(child: Text(model.address, style: normalTextColor28_2)),
                ]),
                gapH8,
                Row(children: [
                  Text("${MultiLanguage.deliveryTime.tr} : ",
                      style: sixthTextColor28),
                  Text(model.stationTime, style: normalTextColor28),
                ]),
              ],
            ),
          )
        ]),
      ));
}
